
#include "qyThread.h"

//-----------------------------------------------------------------------
qyThread::qyThread()
: m_threadHandle( 0 )
{

}

//-----------------------------------------------------------------------
qyThread::~qyThread()
{
	if ( IsRunning() )
	{
		Stop();
	}
}

//-----------------------------------------------------------------------
void qyThread::Start()
{
	if ( IsRunning() )
		return;

	m_threadHandle = (HANDLE)::_beginthreadex( 
		NULL,
		m_stackSize,
		Thread_Proc,
		(LPVOID) this,
		0,
		NULL
		);

	if ( m_threadHandle == 0 )
		return;

	SetThreadPriority( m_threadHandle, THREAD_PRIORITY_NORMAL );
}

//-----------------------------------------------------------------------
void qyThread::Stop()
{
	if ( IsRunning() )
		return;

	if ( m_threadHandle == 0 )
		return;

	::WaitForSingleObject( m_threadHandle, INFINITE );
	::CloseHandle( m_threadHandle );
	m_threadHandle = 0;
}

//-----------------------------------------------------------------------
unsigned int WINAPI qyThread::Thread_Proc( void* self )
{
	assert( self != 0 );
	qyThread *threadObj = ( qyThread * )self;
	threadObj->Thread_DoWork();
	return 0;
}


